<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dodasso
 * Date: 7/28/13
 * Time: 9:54 PM
 * To change this template use File | Settings | File Templates.
 */

namespace common;

require_once(APP_ROOT . '/modules/log/log.php');
require_once(APP_ROOT . '/modules/log/logCommon.php');
require_once(APP_ROOT . '/modules/common/Utils.php');
require_once(APP_ROOT . '/modules/common/Errors.php');
require_once(APP_ROOT . '/modules/persistance/MeasuringUnitPersistance.php');
require_once(APP_ROOT . '/modules/persistance/LineOfBussinesPersistance.php');

use log\Log;
use log\LogCommon;
use common\utils;
use common\Errors;
use persistance\MeasuringUnitPersistance;
use persistance\LineOfBussinesPersistance;

class CommonModule {
    /**
     * @var MeasuringUnitPersistance
     */
    private $measuringUnitPersistance;

    /**
     * @var LineOfBussinesPersistance
     */
    private $lineOfBussinesPersistance;

    /**
     * @var DBConnector
     */
    private $db;

    /**
     * @var boolean
     */
    private $initialized;

    public function __construct() {
        Log::LOG_COMMON_FB();

        $this->measuringUnitPersistance  = null;
        $this->lineOfBussinesPersistance = null;
        $this->db                        = null;
        $this->initialized               = false;

        Log::LOG_COMMON_FE();
    }

    public function __destruct() {
        Log::LOG_COMMON_FB();
        Log::LOG_COMMON_FE();
    }

    public function init($db) {
        Log::LOG_COMMON_FB();

        if((is_null($db))) {
            Log::LOG_COMMON(LogCommon::logTypeError, "Invalid parameters.");
            return false;
        }

        $this->db                        = $db;
        $this->measuringUnitPersistance  = new MeasuringUnitPersistance();
        $this->lineOfBussinesPersistance = new LineOfBussinesPersistance();
        $this->measuringUnitPersistance->init($this->db);
        $this->lineOfBussinesPersistance->init($this->db);
        $this->initialized = true;

        Log::LOG_COMMON_FE();

        return true;
    }

    public function listMeasuringUnits() {
        Log::LOG_COMMON_FB();

        if((!$this->initialized)) {
            Log::LOG_COMMON(LogCommon::logTypeError, "Not initialized.");
            return array('error'=>ERRORS::INTEGRAL_ERR_COMMON_NOT_INITIALIZED);
        }

        $rs = $this->measuringUnitPersistance->listNotDeleted();
        if($rs['error'] != Errors::INTEGRAL_ERR_NONE) {
            Log::LOG_COMMON(LogCommon::logTypeError, 'Unable to list measuring untis.' .
                ' [' . $rs['error'] . ']');
            return Utils::buildResponse(array('error'=>Errors::INTEGRAL_ERR_COMMON_UNABLE_TO_LIST_MEASURING_UNITS,
                                              'data'=>''));
        }

        Log::LOG_COMMON_FE();

        return Utils::buildResponse(array('error'=>Errors::INTEGRAL_ERR_NONE, 'data'=>$rs['data']));
    }

    public function listLineOfBussiness() {
        Log::LOG_COMMON_FB();

        if((!$this->initialized)) {
            Log::LOG_COMMON(LogCommon::logTypeError, "Not initialized.");
            return array('error'=>ERRORS::INTEGRAL_ERR_COMMON_NOT_INITIALIZED);
        }

        $rs = $this->lineOfBussinesPersistance->listNotDeleted();
        if($rs['error'] != Errors::INTEGRAL_ERR_NONE) {
            Log::LOG_COMMON(LogCommon::logTypeError, 'Unable to list line of bussiness.' .
                ' [' . $rs['error'] . ']');
            return Utils::buildResponse(array('error'=>Errors::INTEGRAL_ERR_COMMON_UNABLE_TO_LIST_LINE_OF_BUSSINESS,
                                              'data'=>''));
        }

        Log::LOG_COMMON_FE();

        return Utils::buildResponse(array('error'=>Errors::INTEGRAL_ERR_NONE, 'data'=>$rs['data']));
    }
}